/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view.layer;

import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import soccerscope.model.ClientScene;
import soccerscope.model.ColorDB;
import soccerscope.model.SceneSet;
import soccerscope.model.Team;
import soccerscope.model.WorldModel;
import soccerscope.util.analyze.PassAnalyzer;
import soccerscope.util.geom.Point2f;
import soccerscope.view.FieldPane;

public class PassLayer extends Layer implements ItemListener {

	public PassLayer(FieldPane fieldPane, boolean enable) {
		super(fieldPane, enable);
		this.drawLeft = true;
		this.drawRight = false;
	}

	@Override
	public String getLayerName() {
		return "Pass";
	}

	private JCheckBox leftBox;

	private JCheckBox rightBox;

	@Override
	public JPanel getConfigPanel() {
		this.leftBox = new JCheckBox("Left", this.drawLeft);
		this.leftBox.addItemListener(this);
		this.rightBox = new JCheckBox("Right", this.drawRight);
		this.rightBox.addItemListener(this);
		JPanel panel = new JPanel();
		panel.add(this.leftBox);
		panel.add(this.rightBox);

		return panel;
	}

	public void itemStateChanged(ItemEvent ie) {
		Object o = ie.getSource();
		this.drawLeft = this.leftBox.isSelected();
		this.drawRight = this.rightBox.isSelected();
		this.fieldPane.repaint();
	}

	private boolean drawLeft;

	private boolean drawRight;

	@Override
	public void draw(Graphics g) {
		if (!this.enable)
			return;

		ClientScene clientScene = this.fieldPane.getScene();
		SceneSet set = WorldModel.getInstance().getSceneSet();

		ArrayList list = PassAnalyzer.getPassList();
		Iterator it = list.iterator();

		while (it.hasNext()) {
			PassAnalyzer.Pass pass = (PassAnalyzer.Pass) it.next();
			if (pass.sender.time >= clientScene.time)
				continue;
			if (pass.side == Team.LEFT_SIDE && this.drawLeft) {
				int offset = -1;
				Point2f spos = ((ClientScene) set.getScene(pass.sender.time)).player[pass.sender.unum
						+ offset].pos;
				Point2f epos = ((ClientScene) set.getScene(pass.receiver.time)).player[pass.receiver.unum
						+ offset].pos;
				g.setColor(ColorDB.getColor("team_l_color").darker());
				// drawLine(g, spos, epos);
				drawArrow(g, spos, epos);
			}
			if (pass.side == Team.RIGHT_SIDE && this.drawRight) {
				int offset = MAX_PLAYER - 1;
				Point2f spos = ((ClientScene) set.getScene(pass.sender.time)).player[pass.sender.unum
						+ offset].pos;
				Point2f epos = ((ClientScene) set.getScene(pass.receiver.time)).player[pass.receiver.unum
						+ offset].pos;
				g.setColor(ColorDB.getColor("team_r_color").darker());
				drawLine(g, spos, epos);
			}
		}
	}
}
